package com.watson.todolist;

import java.util.List;

import com.watson.todolist.database.DatabaseManager;
import com.watson.todolist.database.ToDoListManager;
import com.watson.todolist.to.TodoItem;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapController;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * This is the view to do list item activity, which should display the items name, description, category, date, time, recurrence, alarm and location on the map.
 * @author Jasko
 * @version 1.0
 *
 */
public class ViewTodoItemActivity extends MapActivity{
	
	private MapView mapView;
	
	/**
	 * Method to determine if a route is displayed on the map
	 * @return true is displayed, false otherwise
	 */
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	/**
	 * This method is called when the activity is created.
	 * 
	 * @param savedInstanceState
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.view_to_do_item_layout);
		
		
		
		Button menuButtonMain = (Button) findViewById(R.id.menuButtonMain);
		Button menuButtonAdd = (Button) findViewById(R.id.menuButtonAdd);
		Button menuButtonSearch = (Button) findViewById(R.id.menuButtonSearch);
		Button menuButtonSettings = (Button) findViewById(R.id.menuButtonSettings);

		// change background image of main to do list menu button (current
		// screen)
		menuButtonMain.setBackgroundResource(R.drawable.menu_main_);

		// Set up search todo button listener
		menuButtonSearch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), SearchTodoList.class);
				startActivity(intent);
			}
		});

		// Set up create todo item button listener
		menuButtonAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), AddTodoItem.class);
				startActivity(intent);
			}
		});

		// Set up settings button listener
		menuButtonSettings.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),
						SettingsPageActivity.class);
				startActivity(intent);
			}
		});
		
		
		
		mapView = (MapView) findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);
		
		// retrieving todoitem information
		DatabaseManager dbManager = new DatabaseManager();
		SQLiteDatabase db = dbManager.open(getApplicationContext());
		ToDoListManager todoListManager = new ToDoListManager(db);
				
		Intent i = this.getIntent();
		final long todoItemid = i.getLongExtra("ToDoItemLongId", -1);		
		
		TodoItem currentItem = todoListManager.getTodoListItemByTodoId(todoItemid);
		
		String itemName = currentItem.getTodoItemName();
		String itemDesc = currentItem.getDescription();
		int completedCode = currentItem.getTodoCompleted();
		int categoryCode = currentItem.getTodoItemCategoryCode();
		int recurranceCode = currentItem.getAlarmRecurrenceType();
		long timePrior = currentItem.getPriorAlarmReminderTime();
		long timeCode = currentItem.getTodoTime();
		float latitude = currentItem.getLatitude();
		float longitude = currentItem.getLongitude();
		String location = latitude + ", " + longitude;
		List<Address> list;
		
		//populate all UI objects
		ImageView todoImage = (ImageView) findViewById(R.id.checkImage);
		if (completedCode == 0){ 
			todoImage.setImageResource(R.drawable.unchecked);
		} else {
			todoImage.setImageResource(R.drawable.checked);
		}
		
		TextView nameField = (TextView) findViewById(R.id.todoName);
		nameField.setText(itemName);
		
		TextView descField = (TextView) findViewById(R.id.todoDescription);
		descField.setText("Description: " + itemDesc);
		
		TextView categoryField = (TextView) findViewById(R.id.todoCategory);
		String [] categories = this.getResources().getStringArray(R.array.categorySelect);
		categoryField.setText("Category: " + categories[categoryCode]);
		
		TextView recField = (TextView) findViewById(R.id.todoRecurrance);
		String [] recurrance = this.getResources().getStringArray(R.array.recurranceSelect);
		recField.setText("Recurs: " + recurrance[recurranceCode]);
		
		TextView timeField = (TextView) findViewById(R.id.todoTime);
		timeField.setText("Description: " + timeCode);
		
		TextView statusField = (TextView) findViewById(R.id.todoStatus);
		if(currentItem.getTodoCompleted() == 0){
			statusField.setText("Status: Incomplete");
		}else{
			statusField.setText("Status: Completed");
		}
		
		TextView locationField = (TextView) findViewById(R.id.todoLocation);
		//neeed to find address

		//Now to show the map!!!
		
		if(latitude != -1 && longitude != -1){
			timeField.setText("Location coordinate: " + location);
			
			MapView map = (MapView)findViewById(R.id.mapView);
			map.setBuiltInZoomControls(true);
			//map.setSatellite(true);
			
			List<Overlay> mapOverlays = mapView.getOverlays();
			Drawable drawable = this.getResources().getDrawable(R.drawable.checked);
			CustomItemizedOverlay itemizedoverlay = new CustomItemizedOverlay(drawable, this);
			
			GeoPoint point = new GeoPoint((int) (latitude*1E6),(int) (longitude*1E6));
			OverlayItem overlayitem = new OverlayItem(point, "TodoItem Location", "");
			MapController mc = mapView.getController();
			mc.animateTo(point);
            mc.setZoom(16);
			itemizedoverlay.addOverlay(overlayitem);
			mapOverlays.add(itemizedoverlay);	
		}else{
			timeField.setText("Location coordinate: " + "None");
		}
	}
}
